package leetcode.offer0041;

import java.util.LinkedList;
import java.util.Queue;

public class MovingAverage2 {

	int size;
	Queue<Integer> queue;
	int sum;

	public MovingAverage2(int size) {
		this.size = size;
		this.queue = new LinkedList<>();
		this.sum = 0;
	}

	public double next(int val) {
		this.sum += val;
		this.queue.add(val);
		if (this.queue.size() > size) {
			this.sum -= this.queue.poll();
		}
		return 1d * this.sum / this.queue.size();
	}

}
